package com.xxl.sso.core.login.session;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpSession;

public class SessionManager {
    private static final Map<String, Set<HttpSession>> accessTokenHttpSessionMap = new HashMap<>();

    private SessionManager() {
    }

    public static void put(String accessToken, HttpSession session){
        Set<HttpSession> sessions = accessTokenHttpSessionMap.get(accessToken);
        if (sessions == null) {
            sessions = new HashSet<>();
        }
        sessions.add(session);
        accessTokenHttpSessionMap.put(accessToken, sessions);
    }

    public static Set<HttpSession> get(String accessToken){
        return accessTokenHttpSessionMap.get(accessToken);
    }

    public static void romove(String accessToken){
        accessTokenHttpSessionMap.remove(accessToken);
    }

    public static void romove(String accessToken, HttpSession session){
        Set<HttpSession> sessions = accessTokenHttpSessionMap.get(accessToken);
        if (sessions != null) {
            sessions.remove(session);
        }
    }
}
